package com.wikikii.bannerlib.banner;

import com.wikikii.bannerlib.ResourceTable;
import com.wikikii.bannerlib.banner.bean.BannerInfo;
import com.wikikii.bannerlib.banner.listener.OnBannerItemClickListener;
import com.wikikii.bannerlib.banner.listener.OnLoadImageViewListener;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * LoopAdapterWrapper
 */
public class LoopAdapterWrapper extends PageSliderProvider {
    private final Context context;
    private final ArrayList<BannerInfo> bannerInfos;//banner data
    private final OnBannerItemClickListener onBannerItemClickListener;
    private final OnLoadImageViewListener onLoadImageViewListener;

    private Map<Integer, Component> pageMap = new HashMap<>();     //records all the pages in the ViewPager
    private boolean isAnimation;

    public void setAnimation(boolean animation) {
        isAnimation = animation;
    }

    /**
     * 构造函数
     *
     * @param context                   上下文
     * @param bannerInfos               ArrayList
     * @param onBannerItemClickListener OnBannerItemClickListener
     * @param onLoadImageViewListener   OnLoadImageViewListener
     */
    public LoopAdapterWrapper(Context context, ArrayList<BannerInfo> bannerInfos, OnBannerItemClickListener onBannerItemClickListener, OnLoadImageViewListener onLoadImageViewListener) {
        this.context = context;
        this.bannerInfos = bannerInfos;
        this.onBannerItemClickListener = onBannerItemClickListener;
        this.onLoadImageViewListener = onLoadImageViewListener;
    }


    @Override
    public int getCount() {
        return Short.MAX_VALUE;
    }

    @Override
    public Object createPageInContainer(ComponentContainer container, int position) {
        final int index = position % bannerInfos.size();
        final BannerInfo bannerInfo = bannerInfos.get(index);
        Component child;
        if (onLoadImageViewListener != null) {
            child = onLoadImageViewListener.createImageView(context, isAnimation);
            Image imageView = (Image) child.findComponentById(ResourceTable.Id_iv_loop_banner);
            onLoadImageViewListener.onLoadImageView(imageView, bannerInfo.data);
            container.addComponent(child);
            child.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (onBannerItemClickListener != null) {
                        onBannerItemClickListener.onBannerClick(index, bannerInfos);
                    }
                }
            });
        } else {
            throw new NullPointerException("LoopViewPagerLayout onLoadImageViewListener is not initialize,Be sure to initialize the onLoadImageView");
        }
        pageMap.put(position, child);
        return child;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer container, int position, Object object) {
        container.removeComponent((Component) object);
        object = null;
        container = null;
        pageMap.remove(position);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    /**
     * 获去当前VIew的方法
     *
     * @param position 当前位置
     * @return Component
     */
    public Component getPrimaryItem(int position) {
        return pageMap.get(position);
    }

}